package com.tvunetworks.center.user.service.impl;

import com.tvunetworks.center.user.model.UserRoleMenu;
import com.tvunetworks.center.user.model.param.AddOrRemoveUserRoleMenuParam;
import com.tvunetworks.center.user.service.UserRoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @program: tvucc-aggregator
 * @description: UserRoleMenuServiceImpl
 * @author lebronchen
 * @create: 2019-07-05 14:41
 **/
@Service
@Slf4j
public class UserRoleMenuServiceImpl extends BaseService<UserRoleMenu> implements UserRoleMenuService {

    @Override
    public void addOrRemoveUserRoleMenu(AddOrRemoveUserRoleMenuParam param) {
        String userRole = param.getUserRole();
        if (StringUtils.isNotBlank(param.getAddIds())) {
            String[] menuIds = param.getAddIds().split(",");
            for (String menuId : menuIds) {
                addOrRemoveUserRoleMenu(userRole, menuId, 1);
            }
        }
        if (StringUtils.isNotBlank(param.getRemoveIds())) {
            String[] menuIds = param.getRemoveIds().split(",");
            for (String menuId : menuIds) {
                addOrRemoveUserRoleMenu(userRole, menuId, 0);
            }
        }
    }

    private void addOrRemoveUserRoleMenu(String userRole, String menuId, int enable) {
        UserRoleMenu record = new UserRoleMenu()
                .setUserRole(userRole)
                .setMenuId(menuId);
        UserRoleMenu exist = super.selectExistOne(record);
        if (exist == null) {
            record.setEnable(enable);
            super.insert(record);
        } else {
            exist.setEnable(enable);
            super.update(exist);
        }
    }

}
